package com.union.vehicleassistant.module.main;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.BusUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.union.vehicleassistant.R;
import com.union.vehicleassistant.aop.SingleClick;
import com.union.vehicleassistant.app.AppFragment;
import com.union.vehicleassistant.bean.ChannelBean;
import com.union.vehicleassistant.common.Constant;
import com.union.vehicleassistant.util.SettingUtils;
import com.union.vehicleassistant.util.UnitManager;

import java.text.MessageFormat;
import java.util.List;

public class ModeFragment extends AppFragment<MainActivity> {

    public static final String KEY_MODE = "KEY_MODE";
    public static final String KEY_MODE_POS = "KEY_MODE_POS";
    private TextView tvModeName;
    private TextView tvChannelOne;
    private TextView tvChannelTwo;
    private TextView btnConnect;
    private ProgressBar connectProgress;
    private TextView tvState;
    private ChannelBean bean;
    private int modePos;

    public static ModeFragment newInstance(String mode, int pos) {
        ModeFragment fragment = new ModeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_MODE, mode);
        bundle.putInt(KEY_MODE_POS, pos);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mode;
    }

    @Override
    protected void initView() {
        tvModeName = findViewById(R.id.tv_mode_name);
        tvChannelOne = findViewById(R.id.tv_channel_one);
        tvChannelTwo = findViewById(R.id.tv_channel_two);
        btnConnect = findViewById(R.id.btn_connect);
        connectProgress = findViewById(R.id.connect_progress);
        tvState = findViewById(R.id.tv_state);

        setOnClickListener(R.id.btn_connect);
    }

    @Override
    protected void initData() {
        modePos = getInt(KEY_MODE_POS);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        btnConnect.setVisibility(View.GONE);
        connectProgress.setVisibility(View.VISIBLE);
        postDelayed(() -> {
            tvState.setVisibility(View.VISIBLE);
            connectProgress.setVisibility(View.GONE);
            BusUtils.post(MainActivity.KEY_CODE_APPLY_MODE, bean);
        }, 1200);
    }

    @Override
    public void onResume() {
        super.onResume();
        BusUtils.register(this);

        String modeJson = SettingUtils.getInstance().getPreSettingData();

        List<ChannelBean> channels = new Gson().fromJson(modeJson, new TypeToken<List<ChannelBean>>() {
        }.getType());
        bean = channels.get(modePos);
        String[] modes = getResources().getStringArray(R.array.mode_array);
        bean.setName(modes[modePos]);
        tvModeName.setText(bean.getName());
        tvChannelOne.setText(MessageFormat.format("{0}{1}", getString(R.string.setting_channel_one), UnitManager.getSuitPressure(bean.getChannelOne(), true)));
        tvChannelTwo.setText(MessageFormat.format("{0}{1}", getString(R.string.setting_channel_two), UnitManager.getSuitPressure(bean.getChannelTwo(), true)));

        refreshModeInUse();
    }

    @Override
    public void onPause() {
        super.onPause();
        BusUtils.unregister(this);
    }

    @BusUtils.Bus(tag = Constant.BUS_MODE_CHANGE)
    public void refreshModeState() {
        refreshModeInUse();
    }

    private void refreshModeInUse() {
        ChannelBean mode = SettingUtils.getInstance().getDefaultMode();
        if (mode != null) {
            if (mode.getId() == bean.getId()) {
                tvState.setVisibility(View.VISIBLE);
                connectProgress.setVisibility(View.GONE);
                btnConnect.setVisibility(View.GONE);
                return;
            }
        }

        tvState.setVisibility(View.GONE);
        connectProgress.setVisibility(View.GONE);
        btnConnect.setVisibility(View.VISIBLE);
    }
}
